package com.oa.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joneying.common.web.response.Resp;
import com.oa.core.BaseController;
import com.oa.core.PageResult;
import com.oa.core.model.AttendanceMachineXls;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.entity.AttendanceDetails;
import com.oa.pm.entity.AttendanceReport;
import com.oa.pm.service.IAttendanceReportService;
import com.oa.pm.vo.AttendanceReportListVO;
import com.oa.system.service.IAttendanceDetailsService;
import com.oa.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@Api(description = "考勤报表接口")
@RequestMapping("attendanceReport")
@RestController
public class AttendanceReportController extends BaseController {

    @Autowired
    private IAttendanceDetailsService attendanceDetailsService;

    @Autowired
    private IAttendanceReportService attendanceReportService;

    @Autowired
    private IUserService userService;


    @Description("导入")
    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public Resp importReport(@RequestParam MultipartFile multipartFile, @RequestParam String beginTime, @RequestParam String endTime) throws Exception {
        String filename = multipartFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf(".") + 1);

        if(suffix.equals("xlsx") || suffix.equals("xls")){
            List<AttendanceMachineXls> attendanceMachineXls = ExcelUtil.read(AttendanceMachineXls.class, multipartFile.getOriginalFilename(), multipartFile.getInputStream());
            attendanceDetailsService.importReport(attendanceMachineXls, beginTime, endTime, getLoginUserId(), multipartFile.getOriginalFilename());
            return new Resp().success();
        }

        return new Resp().failure("格式不正确");
    }

    @Description("查询列表")
    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    public Resp<AttendanceReport> getList(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<AttendanceReport> page = attendanceReportService.selectPage(new Page(pageNum, pageSize), new EntityWrapper().orderBy("create_time", false));
        List<AttendanceReportListVO> attendanceReportListVOS = new ArrayList<>();
        page.getRecords().forEach(d -> {
            AttendanceReportListVO attendanceReportListVO = new AttendanceReportListVO();
            BeanUtils.copyProperties(d, attendanceReportListVO);

            attendanceReportListVO.setName(userService.selectById(d.getUserId()).getName());
            attendanceReportListVOS.add(attendanceReportListVO);
        });
        return new Resp().success(new PageResult<>(attendanceReportListVOS, page));
    }

    @Description("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Resp delReport(@PathVariable Long id){
        if(attendanceDetailsService.delete(new EntityWrapper().eq("attendance_report_id", id))){
            attendanceReportService.deleteById(id);
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Description("查询详情")
    @ApiOperation(value = "查询详情")
    @GetMapping("/details/list")
    public Resp<AttendanceDetails> getListDetails(@RequestParam Long id, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<AttendanceDetails> page = attendanceDetailsService.selectPage(new Page(pageNum, pageSize), new EntityWrapper().eq("attendance_report_id", id));



        return new Resp().success(new PageResult<>(page.getRecords(), page));
    }

    @Description("导出")
    @ApiOperation(value = "导出")
    @GetMapping("/export/{id}")
    public void export(@PathVariable Long id) throws Exception {
        attendanceDetailsService.export(id, response);
    }
}
